package com.zhaoxi.project.sys.service;

import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.project.sys.mapper.WorkFlowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WorkFlowService extends BaseService {

    @Autowired
    private WorkFlowMapper sysWorkFlowMapper;

    //查询数据集合
    public PageData getList(PageData pd) {

        List<PageData> list = sysWorkFlowMapper.getList(pd);
        return this.responses(list);

    }

    //查询数据对象 通过id
    public PageData getById(PageData pd) {
        PageData pd1 = new PageData();
        if (pd.getLong("id") == null){
            return null;
        }
        pd1.put("id",pd.getLong("id"));
        List<PageData> list = sysWorkFlowMapper.getList(pd1);
        if (list.size() > 0){
            return list.get(0);
        }
        return null;
    }
    //查询数据行数
    public PageData getCount(PageData pd) {

        return this.success(sysWorkFlowMapper.getCount(pd));

    }


    //新增数据
//    public int add(PageData pd) {
//        pd.put("createBy",this.getUsername());
//        return sysWorkFlowMapper.add(pd);
//    }

    public PageData add(PageData pd) {
//    获取返回的主键id
        int row = sysWorkFlowMapper.add(pd);
        if (row == 0) {
            return this.responses(row);

        }
        List<PageData> list = pd.getListPd("line");
        int i = 0;
        if(list != null) {
            while (list.size() > i) {
                PageData pd1 = list.get(i);
                pd1.put("id", pd.getLong("id"));
                this.addLine(pd1);
                i++;
            }
            return this.responses(row);
        }
          return this.responses(1);
    }

    //修改数据
//    public int edit(PageData pd) {
//        pd.put("updateBy",this.getUsername());
//        return sysWorkFlowMapper.edit(pd);
//    }

    //修改编辑数据，同时如果有子单据，也要处理
    public PageData edit(PageData pd) {
        Object line = pd.get("line");
        pd.put("updateBy", this.getUsername());
        int row = sysWorkFlowMapper.edit(pd);
        if (row == 0) {
            return this.responses(row);
        }
        Long id = pd.getLong("id");
        List<PageData> list = pd.getListPd("line");
        System.out.println(list);
        if(list != null) {
            for (PageData pd1 : list) {
                if (pd1.getLong("id") == null) {
                    pd1.put("id", id);
                    this.addLine(pd1);
                } else {
                    this.editLine(pd1);
                }
            }
        }
        return this.responses(row);

    }

    //删除数据
//    public int del(Long id) {
//        return sysWorkFlowMapper.del(id);
//    }

    //删除数据
    public PageData del(Long id) {

        if (id == null){
            return this.error("删除单据id不能为空");

        }
        PageData pd = new PageData();
        pd.put("id",id);
        sysWorkFlowMapper.delLine(pd);
        return this.responses(sysWorkFlowMapper.del(id));

    }

    //子表数据的增删改查================================
    //查询数据集合
    public List<PageData> getLineList(PageData pd) {
        return sysWorkFlowMapper.getLineList(pd);
    }
    //查询数据行数
    public PageData getLineCount(PageData pd) {
        return this.success(sysWorkFlowMapper.getLineCount(pd));
    }
    //新增数据
    public PageData addLine(PageData pd) {
        pd.put("createBy",this.getUsername());
        return this.responses(sysWorkFlowMapper.addLine(pd));
    }
    //修改
    public PageData editLine(PageData pd) {
        pd.put("updateBy",this.getUsername());
        return this.responses(sysWorkFlowMapper.editLine(pd));
    }
//    //删除
//    public int delLine(Long lineId) {
//        return sysWorkFlowMapper.delLine(lineId);
//    }

    //删除
    public PageData delLine(PageData pd) {

        return this.responses(sysWorkFlowMapper.delLine(pd));

    }

    //审批
    public PageData authorize(Long lineId, int result) {
        // 当前用户的roleid
        Long roleId = getLoginUser().getUser().getRoleId();
        // 数据库中对应lineId
        Long roleIdByLineId = sysWorkFlowMapper.getRoleIdByLineId(lineId);
        if (null != roleId && !roleId.equals(roleIdByLineId)){
            throw  new RuntimeException("该用户无此操作权限");
        }

        if(result == 0) {
            PageData pageDataDb =  sysWorkFlowMapper.getOneByline(lineId);
            int orderNum = (int)pageDataDb.get("order_num");
            Integer id = (Integer) pageDataDb.get("id");
            //插入显示的状态log
            int sourceId = sysWorkFlowMapper.getSourceId(lineId);
            PageData dataLog =new PageData();
            dataLog.put("lineId",lineId);
            dataLog.put("result",result);
            dataLog.put("sourceId",sourceId);
            dataLog.put("remark","当前状态");

            //插入下一个审批记录
            PageData nextPageData =  sysWorkFlowMapper.getOneByIdOrder(id,orderNum+1);
            if(nextPageData != null) {
            PageData dataLog1 =new PageData();
            dataLog1.put("lineId",nextPageData.get("line_id"));
            dataLog1.put("result",2);
            dataLog1.put("sourceId",sourceId);
            dataLog1.put("remark","当前状态");
            sysWorkFlowMapper.delLogByLine(lineId);
            sysWorkFlowMapper.insertLog(dataLog).intValue();
                return  this.responses(sysWorkFlowMapper.insertLog(dataLog1).intValue());
            }
        } else if (result == 1) {//不同意回退上一个状态
                return  this.responses(sysWorkFlowMapper.delLogByLine(lineId));
            }


        return  this.responses(1);
    }
}
